package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.entity.CatalogueHazardousTree;
import com.ruoyi.system.mapper.CatalogueHazardousTreeMapper;
import com.ruoyi.system.service.CatalogueHazardousTreeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 危化_文件管理目录树(CatalogueHazardousTree)表服务实现类
 * @since 2023-04-19 17:56:34
 */
@Service("catalogueHazardousTreeService")
public class CatalogueHazardousTreeServiceImpl implements CatalogueHazardousTreeService {
    @Resource
    private CatalogueHazardousTreeMapper catalogueHazardousTreeMapper;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CatalogueHazardousTree queryById(Integer id) {
        return this.catalogueHazardousTreeMapper.queryById(id);
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @Override
    public List<CatalogueHazardousTree> queryByPage(CatalogueHazardousTree catalogueHazardousTree) {
        return catalogueHazardousTreeMapper.queryAllByLimit(catalogueHazardousTree);
    }

    /**
     * 新增数据
     * @param catalogueHazardousTree 实例对象
     * @return 实例对象
     */
    @Override
    public CatalogueHazardousTree insert(CatalogueHazardousTree catalogueHazardousTree) {
        catalogueHazardousTree.setCreatedDate(DateUtils.getNowDate());
        catalogueHazardousTree.setYn(1);
        this.catalogueHazardousTreeMapper.insert(catalogueHazardousTree);
        return catalogueHazardousTree;
    }

    /**
     * 修改数据
     * @param catalogueHazardousTree 实例对象
     * @return 实例对象
     */
    @Override
    public CatalogueHazardousTree update(CatalogueHazardousTree catalogueHazardousTree) {
        catalogueHazardousTree.setModifyDate(DateUtils.getNowDate());
        this.catalogueHazardousTreeMapper.update(catalogueHazardousTree);
        return this.queryById(catalogueHazardousTree.getId());
    }

    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.catalogueHazardousTreeMapper.deleteById(id) > 0;
    }


    /**
     * 树
     * @return
     */
    @Override
    public List<CatalogueHazardousTree> tree() {
        CatalogueHazardousTree catalogueHazardousTree = new CatalogueHazardousTree();
        catalogueHazardousTree.setPid(0);
        List<CatalogueHazardousTree> catalogueTreeList = catalogueHazardousTreeMapper.queryAll(catalogueHazardousTree);
        if (!CollectionUtils.isEmpty(catalogueTreeList)) {
            catalogueTreeList.forEach(value -> {
                CatalogueHazardousTree hazardousTree = new CatalogueHazardousTree();
                hazardousTree.setPid(value.getId());
                List<CatalogueHazardousTree> hazardousTreeList = catalogueHazardousTreeMapper.queryAll(hazardousTree);
                value.setTree(hazardousTreeList);
            });
        }
        return catalogueTreeList;
    }
}
